/*
+--------------------------------------------------------------------------
|   Mblog [#RELEASE_VERSION#]
|   ========================================
|   Copyright (c) 2014, 2015 mtons. All Rights Reserved
|   http://www.mtons.com
|
+---------------------------------------------------------------------------
*/
package mblog.web.controller.desk.account;

import mblog.base.context.AppContext;
import mblog.base.utils.FilePathUtils;
import mblog.base.utils.ImageHandleUtils;
import mblog.core.data.AccountProfile;
import mblog.core.persist.service.UserService;
import mblog.web.controller.BaseController;
import mblog.web.controller.desk.Views;
import mtons.modules.pojos.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.io.File;

/**
 * @author langhsu
 */
@Controller
@RequestMapping("/account")
public class DonationController extends BaseController {
    @Autowired
    private AppContext appContext;
    @Autowired
    private UserService userService;

    private static int imageScale = 200;

    @RequestMapping(value = "/donation", method = RequestMethod.GET)
    public String view() {
        return getView(Views.ACCOUNT_DONATION);
    }

    @RequestMapping(value = "/donation", method = RequestMethod.POST)
    public String post(String path, Float x, Float y, Float width, Float height, ModelMap model) {
        AccountProfile profile = getSubject().getProfile();

        if (StringUtils.isEmpty(path)) {
            model.put("data", Data.failure("请选择图片"));
            return getView(Views.ACCOUNT_DONATION);
        }

        if (width != null && height != null) {
            String root = fileRepoFactory.select().getRoot();
            File temp = new File(root + path);
            File scale = null;

            // 目标目录
            String ava200 = appContext.getDonationDir() + getAvaPath(profile.getId(), imageScale);
            String dest = root + ava200;
            try {
                // 判断父目录是否存在
                File f = new File(dest);
                if (!f.getParentFile().exists()) {
                    f.getParentFile().mkdirs();
                }
                // 在目标目录下生成截图
                String scalePath = f.getParent() + "/" + profile.getId() + ".jpg";
                ImageHandleUtils.truncateImage(temp.getAbsolutePath(), scalePath, x.intValue(), y.intValue(), width.intValue());

                // 对结果图片进行压缩
                ImageHandleUtils.scaleImage(scalePath, dest, imageScale);

                AccountProfile user = userService.updateDonation(profile.getId(), ava200);
                putProfile(user);

                scale = new File(scalePath);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                temp.delete();
                if (scale != null) {
                    scale.delete();
                }
            }
        }
        return "redirect:/account/profile";
    }

    public String getAvaPath(long uid, int size) {
        String base = FilePathUtils.getAvatar(uid);
        return String.format("/%s_%d.jpg", base, size);
    }
}
